/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.adapter;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleColorPool;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.List;

/**
 * Created by Liu ZhuangFei on 2018/6/18. BaseAdapter
 *
 */
public class NonViewAdapter extends BaseItemProvider {
    List<Schedule> schedules;
    LayoutScatter inflater;

    /**
     * NonViewAdapter
     *
     * @param context
     * @param schedules
     */
    public NonViewAdapter(Context context, List<Schedule> schedules) {
        this.schedules = schedules;
        inflater = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return schedules.size();
    }

    @Override
    public Object getItem(int i) {
        return schedules.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertView, ComponentContainer viewGroup) {
        Component mView = null;
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.parse(ResourceTable.Layout_item_nonview,null,false);
            holder.nameTextView = (Text) convertView.findComponentById(ResourceTable.Id_id_nonview_name);
            holder.colorTextView = (Text) convertView.findComponentById(ResourceTable.Id_id_nonview_color);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Schedule schedule = (Schedule) getItem(i);
        ScheduleColorPool colorPool = new ScheduleColorPool();
        holder.nameTextView.setText(schedule.getName());
        RgbColor colorAuto = colorPool.getColorAuto(schedule.getColorRandom());
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(colorAuto);
        holder.colorTextView.setBackground(shapeElement);
        return convertView;
    }

    /**
     * ViewHolder
     *
     * @since 2021-03-29
     */
    static class ViewHolder {
        Text nameTextView;
        Text colorTextView;
    }
}
